/*业务逻辑(左侧加号展开阅读):
 *	1. 获取前端的手机号和密码
 *	2. 查询数据库，检索是否存在该用户
 *		1) 若存在，则允许登录，进入首页
 *		2) 若不存在，则不允许登录，进入登录失败页面
 */


package OSMS.action.Boss;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.*;
import OSMS.entity.*;

public class Boss_Login_Servlet extends HttpServlet {
	
	@Override
	protected void doPost(
			HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
	
//---------------------1. 获取前台手机号和密码 -----------------------//
		BossDao bdao = new BossDao();
		String btel = request.getParameter("tel");						//手机号
		String bpwd = request.getParameter("password");					//密码

		
//---------------------2. 根据前台数据查询数据库----------------------//
		Boss boss = null;
		try {
			boss = bdao.getBossWhenLogin(btel, bpwd);					//根据手机号和密码联合检索用户
		} catch (Exception e) {
			e.printStackTrace();
		}

//---------------------3. 根据查询结果判断是否允许访问首页--------------//
		if(boss!=null) {		//对象非空，说明查询到该用户，允许登录				
			HttpSession session = request.getSession();
			session.setAttribute("boss", boss); 								//将用户信息添加至session
			
			request.getRequestDispatcher("bossindex").forward(request, response);//跳转至首页
			
		} else {			// 对象为空说明数据错误，登录失败，跳转至登录失败界面后重新输入
			request.getRequestDispatcher("fail_to_login.jsp").forward(request, response);
		}
		
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	

	
}
